package com.mornd.gulimall.coupon.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.mornd.gulimall.apis.coupon.to.MemberPriceTo;
import com.mornd.gulimall.apis.coupon.to.SkuReductionTo;
import com.mornd.gulimall.coupon.entity.MemberPrice;
import com.mornd.gulimall.coupon.entity.SkuFullReduction;
import com.mornd.gulimall.coupon.entity.SkuLadder;
import com.mornd.gulimall.coupon.mapper.SkuFullReductionMapper;
import com.mornd.gulimall.coupon.service.IMemberPriceService;
import com.mornd.gulimall.coupon.service.ISkuFullReductionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mornd.gulimall.coupon.service.ISkuLadderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.mornd.gulimall.common.core.utils.BigDecimals.isValid;

/**
 * <p>
 * 商品满减信息 服务实现类
 * </p>
 *
 * @author mornd
 * @since 2023-08-05
 */
@Service
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionMapper, SkuFullReduction> implements ISkuFullReductionService {
    @Resource
    private ISkuLadderService ladderService;
    @Resource
    private IMemberPriceService memberPriceService;

    @Override
    public void saveSkuReductions(List<SkuReductionTo> skuReductionTo) {
        for (SkuReductionTo to : skuReductionTo) {
            // 保存折扣信息
            if (to.getFullCount() != null && to.getFullCount() > 0 && isValid(to.getDiscount())) {
                SkuLadder skuLadder = new SkuLadder();
                skuLadder.setSkuId(to.getSkuId());
                skuLadder.setFullCount(to.getFullCount());
                skuLadder.setDiscount(to.getDiscount());
                skuLadder.setAddOther(to.getDiscountAppend() == 1);
                // todo 折后价
                ladderService.save(skuLadder);
            }

            if (isValid(to.getFullPrice()) && isValid(to.getReducePrice())) {
                // 保存满减信息
                SkuFullReduction fullReduction = new SkuFullReduction();
                fullReduction.setSkuId(to.getSkuId());
                fullReduction.setFullPrice(to.getFullPrice());
                fullReduction.setReducePrice(to.getReducePrice());
                fullReduction.setAddOther(to.getFullAppend() == 1);

                super.save(fullReduction);
            }

            // 保存会员价格
            List<MemberPriceTo> memberPrice = to.getMemberPrice();
            if (CollUtil.isNotEmpty(memberPrice)) {
                List<MemberPrice> mps = memberPrice.stream().map(item -> {
                    MemberPrice mp = new MemberPrice();
                    mp.setSkuId(to.getSkuId());
                    mp.setMemberLevelName(item.getName());
                    mp.setMemberLevelId(item.getId());
                    mp.setMemberPrice(item.getPrice());
                    mp.setAddOther(true);
                    return mp;
                }).collect(Collectors.toList());

                memberPriceService.saveBatch(mps);
            }
        }
    }
}
